package cn.dapeng.interview;

import java.util.Stack;

/**
 * 设计一个含有getMin()功能的栈
 * 1. 支持push, pop, getMin
 */
public class Stack_GetMin_T01 {

    private final Stack<Integer> data = new Stack<>();

    private final Stack<Integer> min = new Stack<>();

    public void push(Integer t) {
        data.push(t);
        if (min.isEmpty()) {
            min.push(t);
        } else {
            Integer item = min.peek();
            if(t <= item) {
                min.push(t);
            }
        }
    }

    public Integer pop() {
        Integer item = data.pop();
        if(item == getMin()) {
            min.pop();
        }
        return item;
    }

    public Integer getMin() {
        if(min.isEmpty()) {
            return null;
        }
        return min.peek();
    }

    public static void main(String[] args) {
        Stack_GetMin_T01  stack = new Stack_GetMin_T01();
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(1);
        stack.push(2);
        stack.push(1);

        while (!stack.data.isEmpty()) {
            Integer item = stack.pop();
            System.out.println("current value :: " + item);

            System.out.println("min value :: " + stack.getMin());
            System.out.println(" ******************************** ");
        }
    }

}
